﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using InHD.model;
using System.Windows.Input;
using InHD.commands;
using InHD.utilities.enums;

namespace InHD.viewmodels
{
    class SupplierFormViewModel : WorkspaceViewModel, IDataErrorInfo
    {
        #region Constructor
        public SupplierFormViewModel(ActionModes mode, Supplier supplier)
        {
            this.FormMode = mode;
            _supplier = supplier;
        }
        #endregion

        #region Value Property
        private Supplier _supplier;
        public string commandExcute = string.Empty;
        bool CanDelete
        {
            get { return !string.IsNullOrEmpty(this._supplier.SupplierUNK) ? true : false; }
        }

        public ActionModes FormMode
        {
            get;
            set;
        }
        #endregion

        #region Presentation Property

        public Supplier Supplier
        {
            get
            {
                return _supplier;
            }
        }

        public string SupplierCode
        {
            get { return _supplier.SupplierCode; }
            set
            {
                if (value == _supplier.SupplierCode)
                    return;

                _supplier.SupplierCode = value;

                base.OnPropertyChanged("SupplierCode");
            }
        }

        public string Name
        {
            get { return _supplier.Name; }
            set
            {
                if (value == _supplier.Name)
                    return;

                _supplier.Name = value;

                base.OnPropertyChanged("Name");
            }
        }

        public int DeliveryDelay
        {
            get { return _supplier.DeliveryDelay; }
            set
            {
                if (value == _supplier.DeliveryDelay)
                    return;

                _supplier.DeliveryDelay = value;

                base.OnPropertyChanged("DeliveryDelay");
            }
        }

        public bool FreeDelivery
        {
            get { return _supplier.FreeDelivery; }
            set
            {
                if (value == _supplier.FreeDelivery)
                    return;

                _supplier.FreeDelivery = value;

                base.OnPropertyChanged("FreeDelivery");
            }
        }

        public string Contact
        {
            get { return _supplier.Contact; }
            set
            {
                if (value == _supplier.Contact)
                    return;

                _supplier.Contact = value;

                base.OnPropertyChanged("Contact");
            }
        }

        public string Phone
        {
            get { return _supplier.Phone; }
            set
            {
                if (value == _supplier.Phone)
                    return;

                _supplier.Phone = value;

                base.OnPropertyChanged("Phone");
            }
        }

        public string Fax
        {
            get { return _supplier.Fax; }
            set
            {
                if (value == _supplier.Fax)
                    return;

                _supplier.Fax = value;

                base.OnPropertyChanged("Fax");
            }
        }

        public string Email
        {
            get { return _supplier.Email; }
            set
            {
                if (value == _supplier.Email)
                    return;

                _supplier.Email = value;

                base.OnPropertyChanged("Email");
            }
        }

        public string Address
        {
            get { return _supplier.Address; }
            set
            {
                if (value == _supplier.Address)
                    return;

                _supplier.Address = value;

                base.OnPropertyChanged("Address");
            }
        }

        #endregion

        #region IDataErrorInfo Interface
        string IDataErrorInfo.Error
        {
            get { return (_supplier as IDataErrorInfo).Error; }
        }

        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                string error = null;

                error = (_supplier as IDataErrorInfo)[propertyName];


                // Dirty the commands registered with CommandManager,
                // such as our Save command, so that they are queried
                // to see if they can execute now.
                CommandManager.InvalidateRequerySuggested();
                return error;
            }
        }
        #endregion

        #region Command
        private RelayCommand _closeCommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(
                        (param) => this.Close());
                }
                return _closeCommand;
            }
        }

        private RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(
                        (param) => this.Save(), (param) => this._supplier.IsValid);
                }
                return _saveCommand;
            }
        }

        private RelayCommand _deleteCommand;
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new RelayCommand(
                        (param) => this.Delete(), (param)=>this.CanDelete);
                }
                return _deleteCommand;
            }
        }
        #endregion

        #region Command Method
        void Close()
        {
            commandExcute = "Close";
            OnRequestClose();
        }

        void Save()
        {
            commandExcute = "Save";
            OnRequestClose();
        }

        void Delete()
        {
            commandExcute = "Delete";
            OnRequestClose();
        }
        #endregion
    }
}
